#!/usr/bin/env python
# -*- coding: UTF-8 -*-
"""
@Project ：turbo 
@File    ：init_samples.py
@Author  ：赵加响
@Date    ：2024/6/20 9:23 
@Desc    ：
"""
from django.core.management import BaseCommand

from apps.codetable.models import CodeTableHead, CodeTableDetail
from apps.rbac.models import Menu, MenuOperate, Operate


class Command(BaseCommand):
    help = 'Initialize Samples DB Data'

    def handle(self, *args, **options):
        self.stdout.write(self.style.SUCCESS('Initialize Samples DB.'))
        inquiry = Operate.objects.get(code='INQUIRY')
        create = Operate.objects.get(code='CREATE')
        update = Operate.objects.get(code='UPDATE')
        delete = Operate.objects.get(code='DELETE')
        release = Operate.objects.get(code='RELEASE')
        close = Operate.objects.get(code='CLOSE')
        samples, created = Menu.objects.get_or_create(code='SAMPLE-1000',
                                                      defaults={'name': '开发示例', 'name_en': 'Samples',
                                                                'content': '前端开发示例'})

        sample_inquiry, created = MenuOperate.objects.get_or_create(menu=samples, operate=inquiry)
        sample_create, created = MenuOperate.objects.get_or_create(menu=samples, operate=create)
        sample_update, created = MenuOperate.objects.get_or_create(menu=samples, operate=update)
        sample_delete, created = MenuOperate.objects.get_or_create(menu=samples, operate=delete)
        sample_release, created = MenuOperate.objects.get_or_create(menu=samples, operate=release)
        sample_close, created = MenuOperate.objects.get_or_create(menu=samples, operate=close)

        sample_inquiry.menu_operate_urls.get_or_create(description='示例 - 查询', url='/samples/list/', method='GET')
        sample_create.menu_operate_urls.get_or_create(description='示例 - 新建', url='/samples/list/', method='POST')
        sample_update.menu_operate_urls.get_or_create(description='示例 - 更新', url='/samples/list/', method='PUT')
        sample_inquiry.menu_operate_urls.get_or_create(description='示例 - 删除', url='/samples/list/', method='DELETE')
        sample_inquiry.menu_operate_urls.get_or_create(description='示例 - 生效', url=r'/samples/list/\d+/release/',
                                                       method='PUT')
        sample_inquiry.menu_operate_urls.get_or_create(description='示例 - 关闭', url=r'/samples/list/\d+/close/',
                                                       method='PUT')

        self.stdout.write(self.style.SUCCESS('Initialize Code Table DB.'))
        codetable, created = CodeTableHead.objects.get_or_create(model='Samples', field='status')
        CodeTableDetail.objects.get_or_create(codetable_head=codetable, localization='zh-hans', code='O', name='开放')
        CodeTableDetail.objects.get_or_create(codetable_head=codetable, localization='zh-hans', code='R', name='生效')
        CodeTableDetail.objects.get_or_create(codetable_head=codetable, localization='zh-hans', code='C', name='关闭')
        CodeTableDetail.objects.get_or_create(codetable_head=codetable, localization='en', code='O', name='Open')
        CodeTableDetail.objects.get_or_create(codetable_head=codetable, localization='en', code='R', name='Release')
        CodeTableDetail.objects.get_or_create(codetable_head=codetable, localization='en', code='C', name='Close')
        self.stdout.write(self.style.SUCCESS('Initialize Samples DB Successful.'))
